﻿var current_op_type;

$(function(){
    //初始化表格数据
	  var res_tree_Table = $('#res_sel_table').treegrid({
	       fit: true,
	       striped: true,
	       rownumbers: true,
	       singleSelect: true,
	       pagination: false,
	       checkbox: false,
	       toolbar: '#tb',
	       idField: 'sysResCode', treeField: 'sysResName',parentField : 'parentCode', iconCls: 'icon-ok',
	       animate: true, 
	       collapsible: true, fitColumns: true, 
	       cascadeCheck:false,
	       url: _global_web_app_path+'/system/resource/res_tree_list',
	       method: 'get',
	       border: false,
	       frozenColumns:[[
	                       {title:'功能编码',field:'sysResCode',width:200, sortable: false,
	                           formatter:function(value){
	                               return '<span style="color:red">'+value+'</span>';
	                           }
	                       }
	                   ]],        
	       columns:  [[
	                   { field: 'sysResName', title: '功能名称', width: 180, sortable: false},
	                   { field: 'sysResUrl', title: '功能路径', width: 180, sortable: false },
	                   { field: 'resLevel', title: '功能级别', width: 180, sortable: false },
	                   { field: 'parentCode', title: '上级功能编码', width: 180, sortable: false }
	                   ]]
	   });
    $('#btn_add').click(function(){add_new_data()});
    $('#btn_edit').click(function(){edit_data()});
    $('#btn_delete').click(function(){delete_data()});
    
   var dialogObj = $('#dd').dialog({
        closed: true,
        cache: false,
        modal: true,
        buttons: [{
            text: '保存',
            iconCls: 'icon-ok',
            handler:function(){
            	
            	var valid_status=$("#edit_form").form("validate");
            	if (!valid_status){
            		return ;
            	}
            	var postData =$("#edit_form").serialize();
                    //ajax调用
                    $.ajax({
                        type: "post",
                        dataType: "json",
                        url:  (current_op_type=='add')?_global_web_app_path+"/system/resource/create":_global_web_app_path+"/system/resource/edit",
                        data: postData,
                        error: function (e) {
                            $.messager.alert('错误', e.responseText, 'error');
                        },
                        success: function (data) {
                            $.messager.alert('提示', data.msg);
                            dialogObj.dialog('close');
                            $('#res_sel_table').treegrid("reload");
                        }
                    });
            	
            }
        }, {
            text: '取消',
            iconCls: 'icon-cancel',
            handler: function () {
                $('#dd').dialog('close');
            }
        }]
    });

});

function add_new_data(){
	current_op_type='add';
	$('#edit_form').form('reset');
	$('#dd').dialog('open');
}

function edit_data(){
	current_op_type='edit';
	 var selected_row = $('#res_sel_table').treegrid('getSelected');
	 if (!selected_row) {
	        $.messager.alert('提示', '请选择需要编辑的数据信息', 'error');
	        return;
	}
	$('#edit_form').form('load',selected_row);
	$('#dd').dialog('open');
}


function format_data_status(val,row){
	if (true==val){
		return "启用";
	}else{
		return "停用";
	}
}

function delete_data(){
	 var selected_row = $('#res_sel_table').treegrid('getSelected');
	 if (!selected_row) {
	        $.messager.alert('提示', '请选择需要删除的数据信息', 'error');
	        return;
	}
    //删除之前给出确认提示
	$.messager.confirm('提示','真的要删除当前选中的数据吗?',function(r){
	   if (r){
			 //ajax调用
		     $.ajax({
		         type: "post",
		         dataType: "json",
		         url:  _global_web_app_path+"/system/resource/delete",
		         data: {resCode:selected_row.sysResCode},
		         error: function (e) {
		             $.messager.alert('错误', e.responseText, 'error');
		         },
		         success: function (data) {
		             $.messager.alert('提示', data.msg);
		             $('#res_sel_table').treegrid("reload");
		         }
		     });	 
	    }
	});
	 
}
